package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int createOrders(String userId, int businessId, double orderTotal, int daId) throws Exception {
        String sql = "INSERT INTO orders(userId,businessId,orderDate,orderTotal,daId) VALUES (?,?,?,?,?)";
        int isRight = 0;
        int count = 0;
        try{
            //进行数据库连接
            con = DBUtil.getConnection();
            //传输数据库语句
            pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            //存放数据
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setDate(3, new java.sql.Date(new java.util.Date().getTime()));
            pst.setDouble(4,orderTotal);
            pst.setInt(5,daId);
            //执行操作
            isRight = pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            //得到生成的订单编号
            if (rs.next()) {
                count = rs.getInt(1);
            }
            return count;
        }finally {
            DBUtil.close(rs,pst);
        }
    }

    @Override
    public Orders getOrdersById(int orderId) throws Exception {
        StringBuffer sql = new StringBuffer();
        Orders orders = null;
//        String sq = "SELECT o.* b.businessId bbusinessId,";
        sql.append(" SELECT o.*, ");
        sql.append("       b.businessId bbusinessId,  ");
        sql.append("       b.businessName bbusinessName, ");
        sql.append("       b.deliveryPrice bdeliveryPrice ");
        sql.append("  FROM orders o LEFT JOIN business b ON o.businessId = b.businessId ");
        sql.append("  WHERE o.orderId=? ");

        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1, orderId);
            rs = pst.executeQuery();
            while (rs.next()) {
                orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                orders.setBusiness(business);
            }
        } finally {//这里不能处理异常，也就是没有catch，只有finally
            //这里负责关闭PreparedStatement和ResultSet
            DBUtil.close(rs, pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        String sql = "SELECT business.businessName,business.businessAddress,business.businessExplain,business.businessImg,business.orderTypeId,business.starPrice,business.deliveryPrice,orderdetailet.*,orders.orderDate,orders.orderTotal,orders.orderState " +
                "FROM business,orderdetailet,orders " +
                "WHERE orders.businessId = business.businessId " +
                "AND orders.orderId =orderdetailet.orderId " +
                "AND orders.userId = ? ";
        ArrayList<Orders> list1 = new ArrayList<>();
        ArrayList<Orderdetailet> list = new ArrayList<>();
        Orders orders = new Orders();
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);

            rs = pst.executeQuery();

            while (rs.next()) {
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                Orderdetailet orderdetailet = new Orderdetailet();
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                orderdetailet.setOdId(rs.getInt("odId"));
                orderdetailet.setOrderId(rs.getInt("orderId"));
                orderdetailet.setFoodId(rs.getInt("foodId"));
                orderdetailet.setQuantity(rs.getInt("quantity"));
                list.add(orderdetailet);
                orders.setBusiness(business);
            }
            orders.setList(list);
            list1.add(orders);
        } finally {                          //这里不能处理异常，也就是没有catch，只有finally
            //这里负责关闭PreparedStatement和ResultSet
            DBUtil.close(rs, pst);
        }
        return list1;
    }
}
